﻿using FM.Core;
using FM.Infrastructure;
using FM.Service;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

namespace FM.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            // Initializes and seeds the database.
            Database.SetInitializer(new FinanceContextInitializer());
            IUnityContainer container = GetUnityContainer();
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
            ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(container));

            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

        private IUnityContainer GetUnityContainer()
        {
            //Create UnityContainer          
            IUnityContainer container = new UnityContainer()
                //.RegisterType<IFormsAuthenticationService, FormsAuthenticationService>()
                //.RegisterType<IMembershipService, AccountMembershipService>()
                //.RegisterInstance<MembershipProvider>(Membership.Provider)
            .RegisterType<FinanceContext>(new HttpContextLifetimeManager<FinanceContext>())
            .RegisterType<IUnitOfWork, UnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>())
            .RegisterType<IUserRepository, UserRepository>(new HttpContextLifetimeManager<IUserRepository>())
            .RegisterType<ICategoryService, CategoryService>(new HttpContextLifetimeManager<ICategoryService>())
            .RegisterType<IExpenseService, ExpenseService>(new HttpContextLifetimeManager<IExpenseService>())
            .RegisterType<ISecurityService, SecurityService>(new HttpContextLifetimeManager<ISecurityService>())
            .RegisterType(typeof(IRepository<>), typeof(RepositoryBase<>));
            return container;
        }
    }
}